package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.bean.UserDetailBean;
import com.example.administrator.yuejian_2.bean.trainorderBean;
import com.example.administrator.yuejian_2.bean.UserBean;
import com.example.administrator.yuejian_2.interservice.Const;
import com.example.administrator.yuejian_2.interservice.UserDetailService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserSettingActivity extends AppCompatActivity {
    private EditText uname;
    private EditText tel;
    private EditText email;
    private EditText pass;
    //private ImageView userpic;
    private Button modify;
    private ImageButton back;
    private int uid;
    public Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_setting);
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo",
                Context.MODE_PRIVATE);
        uid = sharedPreferences.getInt("user_id",0);
        uname = (EditText) findViewById(R.id.uname);
        tel = (EditText) findViewById(R.id.phone);
        email = (EditText) findViewById(R.id.email);
        pass = (EditText) findViewById(R.id.pass);
        //userpic = (ImageView) findViewById(R.id.user_pic);
        back = (ImageButton) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        modify = (Button) findViewById(R.id.modify);
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n = uname.getText().toString();
                String p = pass.getText().toString();
                String t = tel.getText().toString();
                String e = email.getText().toString();
                update(uid,n,p,t,e);
            }
        });
        getUserDetail();

    }
    public void getUserDetail(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserDetailService service = retrofit.create(UserDetailService.class);
        Call<UserDetailBean> call = service.detail(uid);
        call.enqueue(new Callback<UserDetailBean>() {
            @Override
            public void onResponse(Call<UserDetailBean> call, Response<UserDetailBean> response) {
                if (response.body()!= null){
                    uname.setText(response.body().getU_name());
                    tel.setText(response.body().getTel());
                    email.setText(response.body().getEmail());
                    pass.setText(response.body().getU_pass());
//                    String pic = response.body().getU_pic();
//                    if (!pic.equals(""))
//                        Picasso.with(context)
//                                .load("http://10.0.2.2/YueJian16/Public/clubphoto/"+pic)
//                                .into(userpic);
                }
            }

            @Override
            public void onFailure(Call<UserDetailBean> call, Throwable t) {

            }


        });
    }


    public void update(int id,String name,String upass,
                       String utel, String uemail){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        UserDetailService service = retrofit.create(UserDetailService.class);
        Call<UserBean> call = service.update(id,name,upass,utel,uemail);
        call.enqueue(new Callback<UserBean>() {
            @Override
            public void onResponse(Call<UserBean> call, Response<UserBean> response) {
                if (response.body()!= null){
                    Toast.makeText(UserSettingActivity.this,"修改成功",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFailure(Call<UserBean> call, Throwable t) {

            }


        });

    }

}
